package com.angle3d.samples;

import android.content.Context;
import android.graphics.Bitmap;

import com.angle3d.Mesh;
import com.angle3d.MeshUtil;
import com.angle3d.Node;
import com.angle3d.ShaderProgram;
import com.angle3d.ShaderSource;
import com.angle3d.ShaderSource.ShaderType;
import com.angle3d.Texture;

class SceneFactory {
	
	private final static String VertexShaderSource = 
		"precision highp float;" +
		"uniform mat4 ModelView;" +
		"uniform mat4 Projection;" +
		"attribute vec4 Position;" +
        "attribute vec2 TexCoord;" +
        "varying vec2 tcoord;" +
        "void main() {" +
        "tcoord = TexCoord;" + 
        "gl_Position = Projection * ModelView * Position; }";
    
    private final static String FragmentShaderSource = 
        "precision mediump float;" +
        "uniform sampler2D Texture0;" +
        "varying vec2 tcoord;" +
        "void main() {" +
        "vec4 texColor0 = texture2D(Texture0, tcoord);" +
        "gl_FragColor = texColor0 * vec4(1.0, 0.5, 0.0, 1.0); }";
    
    private Mesh createTexturedCube(Context context) {
    	//create a mesh representing a cube
        Mesh mesh = MeshUtil.createBox(1, 1, 1);
		
        //create the vertex and fragment shaders to render our mesh
		ShaderSource vertex = new ShaderSource(VertexShaderSource, ShaderType.VERTEX);
        ShaderSource fragment = new ShaderSource(FragmentShaderSource, ShaderType.FRAGMENT);
        
        //specify the shader program for rendering the mesh
        mesh.setShaderProgram(new ShaderProgram(vertex, fragment));
        
        //create a texture to be applied on the mesh surface
        Texture texture = new Texture();
        Bitmap bm = SampleUtil.loadBitmap(R.drawable.brick, context);
        texture.setBitmap(bm);
        
        //set the texture in the mesh
        //note that the name "Texture0" must match the texture name in the Vertex shader source code
        mesh.setTexture(texture, "Texture0", 0);
        
    	return mesh;
    }
    
	public Node createScene(Context context) {
		//create the scene root node
		Node scene = new Node();
        
		//create a mesh
		Mesh mesh = createTexturedCube(context);
		
        //create a node to host our mesh
        Node node = new Node();
        
        //attach the mesh to the node
        node.addSceneObject(mesh);
        
        //attach our mesh node as child of the scene root
        scene.addChild(node);
        
        //return our scene
        return scene;
	}
}
